var AgentWl = (function($) {

	var memberWlLink = function(cellvalue, options, rowObject) {
		return '<a class="member-wl-link" href="javascript:;" data-agent-id="'
				+ rowObject.agent_id + '" data-agent-name="'
				+ rowObject.username + '">' + cellvalue + '</a>';
	};

	var agentIdModel = {
		name : 'agent_id',
		index : 'agent_id',
		width : 60,
		sorttype : "int",
		hidden : true
	};

	var agentUsernameModel = {
		name : 'username',
		index : 'username',
		width : 60,
		sorttype : "string",
		formatter : memberWlLink
	};
	var agentWlModel = {
		name : 'agent_win_lose',
		index : 'agent_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};
	var hostWlModel = {
		name : 'host_win_lose',
		index : 'host_win_lose',
		width : 80,
		align : "right",
		sorttype : "float",
		formatter : 'currency'
	};

	return {
		initAgentWlEvent : function() {
			$("#submit").click(function() {
				AgentWl.bindAgentWl();
				return false;
			});

			/* open member wl */
			$("body").on(
					"click",
					".member-wl-link",
					function() {
						location.href = "index.php/admin/report/memberwl"
								+ "?agent_id=" + $(this).attr("data-agent-id")
								+ "&agent_name="
								+ $(this).attr("data-agent-name")
								+ "&fromdate=" + $("#fromdate").val()
								+ "&todate=" + $("#todate").val()
								+ "&show_filter=false";
					});
		},
		initAgentWl : function() {		
			Report.initDateRangePicker();
			AgentWl.initAgentWlEvent();
			AgentWl.bindAgentWl();
		},

		bindAgentWl : function() {

			var gridOption = $.extend(Report.gridOption, {
				height : 500,
				colNames : [ 'User Id', "Agent", 'AgentWL', 'HostWl' ],
				colModel : [ agentIdModel, agentUsernameModel, agentWlModel,
						hostWlModel ]
			});

			$.blockUI();
			$.ajax( {
				type : "POST",
				url : "index.php/admin/report/agentwlData",
				dataType : "json",
				data : {
					fromdate : $("#fromdate").val(),
					todate : $("#todate").val()
				},
				success : function(response) {
					// Show the grid
					gridOption.data = response.Main;
					gridOption.userData = response.Total;

					$("#jqgrid-table").jqGrid('GridUnload');
					$("#jqgrid-table").jqGrid(gridOption);
					Report.initColumnChooser();

				},
				complete : function() {
					$.unblockUI()
				}
			});
		}
	};
})(jQuery);
